<template>
  <string-split-format-mul-select
    v-if="mode === 'QUICK' && !isLoading"
    v-model="selects" :filter-function="(val)=>{return val!==''}" :format-function="(val)=>{return val.trim()}"
    :allow-create="allowCreate" :mode="mode" :multiple-limit="multipleLimit" :options="options"
    :placeholder="placeholder"
  />
  <string-split-format-mul-select
    v-else-if="mode === 'NORMAL'"
    v-model="selects" :filter-function="(val)=>{return val!==''}" :format-function="(val)=>{return val.trim()}"
    :allow-create="allowCreate" :mode="mode" :multiple-limit="multipleLimit" :options="options"
    :placeholder="placeholder"
  />
</template>
<script>
import {hiveUdfs} from "@/api/lineage/metaHive";
import {concatAndRemoveDuplicate} from "@/utils/array";
import StringSplitFormatMulSelect from "@/views/common/stringSplitFormatMulSelect.vue";

export default {
  name: "HiveUdfsSelect",
  components: {StringSplitFormatMulSelect},
  props: {
    value: {
      type: Array, default: function () {
        return []
      }
    },
    allowCreate: {type: Boolean, default: false}, mode: {type: String, default: null},
    addStar: {type: Boolean, default: false},
    placeholder: {type: String, default: '请选择function'},
    multipleLimit: {type: Number, default: 0},
  },

  data() {
    return {
      selects: [],
      options: [],
      isLoading: true
    }
  },

  watch: {
    'value': {
      handler(val) {
        this.selects = val
      }, immediate: true
    },

    'selects': {
      handler(val) {
        this.$emit('input', val)
      }, immediate: false
    }
  },

  created() {
    this.list()
  },

  methods: {
    list() {
      this.options = this.addStar ? ['*'] : [];
      hiveUdfs().then(res => {
        this.options = concatAndRemoveDuplicate(this.options, res.data)
        this.isLoading = false
      }).catch(() => {
        this.$message.error("数据加载失败~~~")
      })
    },
  }
}
</script>

<style scoped>

</style>
